package com.ruoyi.web.teacher;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.teacher.vo.TPaperVo;
import com.ruoyi.teacher.service.ITPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 试卷Controller
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Api(tags = "试卷Controller")
@RestController
@RequestMapping("/teacher/paper")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TPaperController extends BaseController {

    private final ITPaperService tPaperService;

    @ApiOperation("查询试卷列表")
    @PreAuthorize("@ss.hasPermi('teacher:paper:list')")
    @GetMapping("/list")
    public TableDataInfo<TPaperVo> list(TPaperVo entity) {
        return tPaperService.queryList(entity);
    }

    @ApiOperation("查询试卷所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TPaperVo entity) {
        return AjaxResult.success("查询成功", tPaperService.queryAll(entity));
    }

    @ApiOperation("导出试卷列表")
    @PreAuthorize("@ss.hasPermi('teacher:paper:export')")
    @Log(title = "试卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TPaperVo entity) {
        List<TPaperVo> list = tPaperService.queryAll(entity);
        ExcelUtil<TPaperVo> util = new ExcelUtil<>(TPaperVo.class);
        util.exportExcel(response, list, "试卷数据");
    }

    @ApiOperation("获取试卷详细信息")
    @PreAuthorize("@ss.hasPermi('teacher:paper:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tPaperService.queryById(id));
    }

    @ApiOperation("新增试卷")
    @PreAuthorize("@ss.hasPermi('teacher:paper:add')")
    @Log(title = "试卷", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TPaperVo entity) {
        tPaperService.savePaper(entity);
        if (entity.getId() != null){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.success("新增失败");
    }

    @ApiOperation("修改试卷")
    @PreAuthorize("@ss.hasPermi('teacher:paper:edit')")
    @Log(title = "试卷", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TPaperVo entity) {
        return toAjax(tPaperService.updateById(entity));
    }

    @ApiOperation("删除试卷")
    @PreAuthorize("@ss.hasPermi('teacher:paper:remove')")
    @Log(title = "试卷", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tPaperService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
